package com.example.smartall.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.smartall.R;
import com.example.smartall.adapter.PervasiveAdapter;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.diaglog.LoadedDialog;
import com.example.smartall.entity.HospitalCategoryBean;
import com.example.smartall.ui.base.BaseActivity;
import com.example.smartall.utils.TextUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * 医院门诊分类
 */
public class HospitalCategoryActivity extends BaseActivity {

    private String patientName;

    private ImageView imBack;
    private TextView tvTitle;
    private ListView lvCategory;

    private List<HospitalCategoryBean.RowsBean> categoryBeans = new ArrayList<>();
    private ListAdapter adapter;
    private MHandler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital_category);

        patientName = getIntent().getStringExtra("cardName");
        if (TextUtils.isEmpty(patientName)) showToast("数据错误");

        handler = new MHandler();

        init();
        initEvent();
        getCategory();
    }

    @Override
    public void init() {
        imBack = (ImageView) findViewById(R.id.im_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        lvCategory = (ListView) findViewById(R.id.lv_category);

        tvTitle.setText("选择挂号科室");
    }

    @Override
    public void initEvent() {
        imBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HospitalCategoryActivity.this.finish();
            }
        });

        lvCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (TextUtils.isEmpty(patientName)) return;
                Intent intent = new Intent(HospitalCategoryActivity.this,HCPRegistrationActivity.class);
                intent.putExtra("patientName",patientName);
                intent.putExtra("category",categoryBeans.get(i));
                startActivity(intent);
            }
        });
    }

    private void getCategory(){
        if (categoryBeans.size() > 0) return;
        final LoadedDialog dialog = new LoadedDialog(HospitalCategoryActivity.this);
        dialog.show();
        Api.config(ApiConfig.HOSPITAL_CATEGORY,null).getRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                HospitalCategoryBean bean = new Gson().fromJson(res,HospitalCategoryBean.class);
                if (bean.getCode() == 200){
                    categoryBeans = bean.getRows();
                    handler.sendEmptyMessage(0x01);
                }
                dialog.dismiss();
            }

            @Override
            public void onFailure(Exception e) {
                showToastSync("加载失败");
                dialog.dismiss();
            }
        });
    }

    class MHandler extends Handler{
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0x01:
                    adapter = new ListAdapter(categoryBeans);
                    lvCategory.setAdapter(adapter);
                    break;
            }
        }
    }

    class ListAdapter extends PervasiveAdapter<HospitalCategoryBean.RowsBean> {

        public ListAdapter(List<HospitalCategoryBean.RowsBean> datas) {
            super(datas);
        }

        @Override
        protected void setUI(ViewHolder holder, int position, Context context) {
            ImageView img = (ImageView) holder.getItemView(R.id.img);
            TextView title = (TextView) holder.getItemView(R.id.title);
            TextView subtitle = (TextView) holder.getItemView(R.id.subtitle);
            TextView content = (TextView) holder.getItemView(R.id.content);

            title.setText(categoryBeans.get(position).getCategoryName());
            content.setText("预约金额："+categoryBeans.get(position).getMoney());
        }

        @Override
        protected int getLayoutId() {
            return R.layout.common_list_item;
        }
    }
}